// pages/index/newsDetail/newsDetail.js
var app = getApp(),
  that;
var WxParse = require('../../../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    specialDetai: [],
    id: '',
    userInfo: '',
    context: '',
    textContext: '',
    specialId:'',
    isshow: false,
    status: '',
    zanzan: '',
    shopDetail:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this

    // var hh = JSON.parse(decodeURIComponent(options.item))
    // console.log(hh[0].carSpecials.id)
    that.setData({
      specialId: JSON.parse(decodeURIComponent(options.item))[0].carSpecials.id,
      status: JSON.parse(decodeURIComponent(options.item))[0].carSpecials.dummyStatus,
      specialDetai: JSON.parse(decodeURIComponent(options.item))[0].carSpecials,
      shopDetail: JSON.parse(decodeURIComponent(options.item))[0].hyParductManageBean
    })
    console.log(JSON.parse(decodeURIComponent(options.item))[0].carSpecials.id)
    this.data.textContext = this.data.specialDetai.specialDetail,
    that.setReading()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  //获取阅读量
  setReading: function () {
    app.getData({
      url: app.api.getSpecialReading(this.data.specialId, wx.getStorageSync('bizUserInfo').id, 1),
      method: "POST",
      ok: function (res) {
        console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var reg = new RegExp('<img', "g")
    this.setData({
      context: this.data.textContext.replace(reg, '<img style="max-width:100%;height:auto"'),
    });
    if (this.data.status == '' || this.data.status == null || this.data.status == 1) {
      this.setData({
        zanzan: 3
      })
    } else {
      this.setData({
        zanzan: 2
      })
    }
    this.getContent()
  },
  getContent:function(){
    that=this;
    app.getData({
      url: app.api.getSpecialDetail(that.data.specialId, wx.getStorageSync('bizUserInfo').id),
      method:'GET',
      ok:function(res){
        console.log(res)
        console.log(res.data.data[0].carSpecials.specialDetail)
        var article = res.data.data[0].carSpecials.specialDetail;
        WxParse.wxParse('article', 'html', article, that, 10);
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    this.shareModalClose()
  },
  showShareModal: function() {
    this.setData({
      share_modal_active: "active",
      no_scroll: !1
    });
  },
  shareModalClose: function() {
    this.setData({
      share_modal_active: "",
      no_scroll: !1
    });
  },
  //点赞
  giveUp(e) {
    console.log(e)
    console.log(that.data.specialDetai.dummyStatus)
    var zan = 3
    if (that.data.specialDetai.dummyStatus == null || that.data.specialDetai.dummyStatus == 1 || that.data.specialDetai.dummyStatus == 3) {
      zan = 2
    } else {
      zan = 3
    }
    this.isPaise(zan)
  },
  // 获取点赞
  isPaise(type) {
    app.getData({
      url: app.api.getSpecialReading(this.data.specialDetai.id, wx.getStorageSync('bizUserInfo').id, type),
      method: 'POST',
      ok: function(res) {
        console.log(res)
        if (res.errMsg == 'request:ok' && res.statusCode == 200) {
          if (res.data.code == 1) {
            if (type == 2) {
              wx.showToast({
                title: '点赞成功！',
                icon: 'none'
              })
              that.data.specialDetai.dummyStatus = 2;
              console.log('f' + that.data.specialDetai.dummyStatus)
              // that.setData({
              //   zanzan: that.data.newsDetail.giveUpStatus
              // })
            } else {
              wx.showToast({
                title: '取消点赞成功！',
                icon: 'none'
              })
              that.data.specialDetai.dummyStatus = 3;
              console.log('f' + that.data.specialDetai.dummyStatus)

            }
            that.setData({
              zanzan: that.data.specialDetai.dummyStatus
            })
          } else {
            wx.showToast({
              title: '操作失败！',
              icon: 'none'
            })
          }
        } else {
          wx.showToast({
            title: res.data.errMsg
          })
        }
      }
    })
  },
  wxParseTagATap:e=>{
    let url = e.currentTarget.dataset.src
    console.log(url)
    wx.redirectTo({
        url: url
      })
  }
})